package com.xhl.oauth2.modules.admin.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.Query;
import com.xhl.oauth2.base.R;
import com.xhl.oauth2.modules.admin.entity.SysUserEntity;
import com.xhl.oauth2.modules.admin.param.SysUserParam;
import com.xhl.oauth2.modules.admin.service.SysUserService;
import com.xhl.oauth2.modules.base.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;



    /**
     * 分页查询
<<<<<<< master
=======
     * @param userName
     * @return
     */
    @GetMapping("/token")
    public R token(String userName) {
        return R.ok();
    }

    /**
     * 分页查询
>>>>>>> local
     * @param param
     * @return
     */
//    @PreAuthorize("hasAuthority('system:admin')")
    @GetMapping("/page")
    public R page(SysUserParam param) {
        Query query = param.toQuery();
        PageInfo<SysUserEntity> pageInfo = sysUserService.page(query);
        return R.ok().put("data", pageInfo);
    }

    /**
     * 保存
     * @param sysUser
     * @return
     */
    @GetMapping("/save")
    public R save(SysUserEntity sysUser) {
        sysUserService.saveIgnoreNull(sysUser);
        return new R().ok().put("data", sysUser.getUserId());
    }


    /**
     * 更新
     * @param sysUser
     * @return
     */
    @GetMapping("/update")
    public R update(SysUserEntity sysUser) {
        sysUserService.updateIgnoreNull(sysUser);
        return R.ok();
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @GetMapping("/delete")
    public R delete(Integer userId) {
        sysUserService.deleteById(userId);
        return R.ok();
    }

    public static void main(String[] args) {
        String t = new BCryptPasswordEncoder().encode("123");
        System.out.println("======:" +t);
    }
}
